package com.ckm.factory;

import com.ckm.context.DynamicMongoContextHolder;
import com.ckm.datasource.DynamicMongoTemplate;
import com.ckm.properties.DynamicMongoProperties;
import com.mongodb.ClientSessionOptions;
import com.mongodb.client.ClientSession;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;

import java.util.HashMap;
import java.util.Map;

@RequiredArgsConstructor
public class DynamicMongoFactory{

    @Getter
    private final Map<String ,MongoDatabaseFactory> mongoDatabaseFactoryMap;

    public void setMongoTemplate(DynamicMongoProperties properties){
        for (Map.Entry<String, MongoProperties> entry : properties.getDynamic().entrySet()) {
            mongoDatabaseFactoryMap.put(entry.getKey(),new SimpleMongoClientDatabaseFactory(entry.getValue().getUri()));
        }
    }

}
